//
//  YGFoodSurveyViewController.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/4/13.
//

#import "YGFoodSurveyViewController.h"
#import "YGFoodTimeSelectView.h"
#import "TTDatePickView.h"
#import "YGFoodTypeSelectView.h"
#import "YGSurveyBLL.h"
#import "YGServerItemCollectionViewCell.h"
#import "YGFoddItemDetailView.h"
@interface YGFoodSurveyViewController ()<UITableViewDelegate,UITableViewDataSource, YGServerItemCollectionViewCellDelegate>
@property (nonatomic, strong) YGFoodTimeSelectView *timeSelectView;
@property (nonatomic, strong) TTDatePickView  *brithPickView;//
@property (nonatomic, strong) NSString  *currrentSelectTime;
@property (nonatomic, strong) YGFoodTypeSelectView *typeSelectView;
@property (nonatomic, strong) UITableView  *tableView;
@property (nonatomic, strong) NSArray  *dataArray;
@property (nonatomic, strong) YGServerFoodItemModel  *itemModel;
@property (nonatomic, strong) YGFoddItemDetailView *detailView;

@end

@implementation YGFoodSurveyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"膳食记录"];
    //设置导航栏属性
    self.navigationController.navigationBar.barTintColor = UIColorFromRGB(0x37C8D7);
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : UIColorFromRGB(0xffffff)}];
    self.currrentSelectTime = [self getCurrentTimes];

    [self initView];
    [self initconstraints];

    [self requestData];
}

-(void)initView{
    [self.view addSubview:self.timeSelectView];
    [self.view addSubview:self.typeSelectView];
    [self.view addSubview:self.tableView];
}

-(void)initconstraints{
    [self.timeSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(10);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(105);
        make.top.mas_equalTo(kNavigationHeight + 10);
    }];
    
    [self.typeSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(22);
        make.top.mas_equalTo(self.timeSelectView.mas_bottom).offset(21);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.typeSelectView.mas_bottom).offset(40);
        make.left.right.bottom.mas_equalTo(self.view);
    }];
}
- (void)requestData{
    WeakSelf(self);
    [YGSurveyBLL requestServerTypeWithSuccess:^(NSArray<YGFoodServerTypeModel *> * _Nonnull model) {
        [weakself.typeSelectView configureModels:model];
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)requestFoodListWithTypeId:(NSString *)typeId;
{
    WeakSelf(self);
    [YGSurveyBLL requestServerWithData:self.currrentSelectTime typeId:typeId Success:^(YGServerFoodModel * _Nonnull model) {
        weakself.dataArray = model.researchDel;
        [weakself.tableView reloadData];
    } fail:^(NSError * _Nonnull error) {
        
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YGServerItemCollectionViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YGServerItemCollectionViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[YGServerItemCollectionViewCell alloc] init];
    }
    cell.delegate = self;
    
    YGServerFoodItemModel *model = [self.dataArray objectAtIndex:indexPath.row];
    [cell configureModel:model];
    return cell;
}

#pragma mark - dateDatate
- (void)ttDatePickEnsureAction:(NSString *)YMd date:(NSDate *)date {
    [self.timeSelectView setDate:YMd];
    self.currrentSelectTime = YMd;
}

#pragma mark - cellDelegate
- (void)serverCellSelectCount:(YGServerFoodItemModel *)model{
    self.itemModel = model;
//    [self requetstItemWithTypeId:nil];
    [YGSurveyBLL requestSave];
}

- (void)serverCellClickDetail:(YGServerFoodItemModel *)model{
    WeakSelf(self);
    [YGSurveyBLL requestServerDetailWithData:self.currrentSelectTime typeId:model.typeId num:model.num Success:^(YGServerDetailModel * _Nonnull model) {
        [weakself.detailView configureModel:model];
        weakself.detailView.hidden = NO;
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (TTDatePickView *)brithPickView {
    if (!_brithPickView) {
        UIView *view = [UIApplication sharedApplication].keyWindow;
        _brithPickView = [[TTDatePickView alloc] initWithFrame:view.bounds];
        _brithPickView.limitAge = 0;
        _brithPickView.delegate = self;
        _brithPickView.atTime = self.currrentSelectTime;
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        
        NSDate *endDate = [formatter dateFromString:@"1945-01-01"];
        
        _brithPickView.minimumDate = endDate;
        _brithPickView.maximumDate = [NSDate date];
        [view addSubview:_brithPickView];
    }
    return _brithPickView;
}

- (YGFoodTypeSelectView *)typeSelectView{
    if (!_typeSelectView) {
        _typeSelectView = [[YGFoodTypeSelectView alloc] init];
        WeakSelf(self);
        _typeSelectView.ClickItemBlock = ^(YGFoodServerTypeModel * _Nonnull model) {
            [weakself requestFoodListWithTypeId:model.value];
        };
    }
    return _typeSelectView;
}

- (YGFoodTimeSelectView *)timeSelectView{
    if (!_timeSelectView) {
        _timeSelectView = [[YGFoodTimeSelectView alloc] init];
        [_timeSelectView setDate:self.currrentSelectTime];
        WeakSelf(self);
        _timeSelectView.ClickCallBlock = ^{
            [weakself.brithPickView show];
        };
    }
    return _timeSelectView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        [_tableView registerClass:YGServerItemCollectionViewCell.class forCellReuseIdentifier:@"YGServerItemCollectionViewCell"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (YGFoddItemDetailView *)detailView{
    if (!_detailView) {
        _detailView = [[YGFoddItemDetailView alloc] init];
        [self.view addSubview:_detailView];
        [_detailView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.bottom.mas_equalTo(0);
        }];
    }
    return _detailView;
}

- (NSString*)getCurrentTimes{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制

    [formatter setDateFormat:@"YYYY-MM-dd"];

    //现在时间,你可以输出来看下是什么格式

    NSDate *datenow = [NSDate date];

    //----------将nsdate按formatter格式转成nsstring

    NSString *currentTimeString = [formatter stringFromDate:datenow];

    NSLog(@"currentTimeString =  %@",currentTimeString);

    return currentTimeString;

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
